"""
@name : 作业.py
@author : chenwenying
@time : 2022/3/11
"""
# #######检测出现次数前十的ip地址
# import random
# # 用来存放ip地址
# lst = []
# # 用来记录ip地址，以及出现的次数
# dic = {}
# with open("chen.txt","a+",encoding="utf-8") as fp:
#     # 清空chen.txt
#     fp.truncate(0)
#     # 循环产生1200个ip地址
#     for i in range(120):
#         a = random.randint(0, 255)
#         b = ".".join(["172.25.254", str(a)])
#         lst.append(b)
#         c = b.replace("'", "")
#         fp.write(c+"\n")
# # 循环统计ip地址出现的次数，用字典保存
# for i in lst:
#     dic[i] = lst.count(i)
# n = 10
# # 对字典的values进行排序，得到一个包含元组的列表
# lst1 = sorted(dic.items(), key=lambda y: y[1], reverse=True)
# for i, j in lst1:
#     n = n-1
#     if n >= 0:
#         print(f'{i} {j}')
#     else:
#         break

# ############词频分析#######
# 方法1
# import string
# # str1保存了所有的非字母字符
# str1 = string.punctuation + '0123456789'
# # 使用utf-8-sig可以避免在文件开头出现\ufeff
# with open("Walden(1).txt","r",encoding="utf-8-sig") as fp:
#     # 将passage.txt中的每一行组成一个列表
#     a = fp.read()
# for i in str1:
#     # 去掉a中非字母字符
#     # a = a.replace(i,' ')
#     # 将a中字母转变为小写
#     a = a.lower()
#     # 将a进行分割
#     lst1 = a.split()
# dict1 = {}
# for j in lst1:
#     # 如果单词已经存入字典，则出现次数+1
#     if j in dict1:
#         dict1[j] = dict1[j] + 1
#     # 如果单词为第一次出现，则为第一次出现
#     else:
#         dict1[j] = 1
# print(dict1)

# 方法2：
# 缺点：时间复杂度太大了，count命令的底层原理也是for循环
# 26个英文单词
# lst = list('abcdefghijklmnopqrstuvwxyz')
# lst1 = []
# with open("Walden(1).txt","r",encoding="utf-8") as fp:
#     # 将passage.txt中的每一行组成一个列表
#     a = fp.readlines()
#     for i in a:
#         # 去掉每一个元素后面的换行符
#         lst1.append(i.strip())
#     # 将lst1变成一个字符串
#     b = " ".join(lst1)
#     # 将大写字母全都转化为小写字母
#     d = b.lower()
#     # 将b中的非字母换成空格
#     for i in d:
#         if i not in lst:
#             c = d.replace(i, ' ')
#             d = c
# # 当不给split函数传递任何参数时，分隔符sep会采用任意形式的空白字符：空格、tab、换行、回车以及formfeed
# lst2 = c.split()
# dic = {}
# # 统计每个元素出现的次数
# for i in lst2:
#     dic[i] = lst2.count(i)
# print(dic)

# ###########3个数运算后结果为24#######
# print('**温馨提示--输入格式为：[1,2,3]')
# lst = eval(input('Please enter three numbers to check whether the sum you want is 24:'))
# # 存放还没存提取的数
# lst2 = lst.copy()
# lst1 = ['+', '-', '*', '/']
# # 用来存放运算结果
# lst4 = []
# for i in lst:
#     lst2.remove(i)
#     for j in lst2:
#         lst2.remove(j)
#         k = lst2[0]
#         for h in lst1:
#             for g in lst1:
#                 d = ''.join([str(i), h, str(j), g, str(k)])
#                 d = int(eval(d))
#                 lst4.append(d)
#     lst2 = lst.copy()
# if 24 in lst4:
#     print('true')
# else:
#     print('false')

# ##########统计单词数量#######
# '''统计字符串中单词的数量'''
# 方法一：
# sentence = "I can because i think i'am can"
# result = {word: sentence.split().count(word) for word in set(sentence.split())}
# print(result)
# result={}

# 方法二：
# '''Counter 是 dictionary 对象的子类。collections 模块中的 Counter() 函数会接收一个诸如 list 或 tuple 的迭代器，
# 然后返回一个 Counter dictionary。这个 dictionary 的键是该迭代器中的唯一元素，每个键的值是迭代器元素的计数。Counter的使用和dictionary
# 类似'''
# from collections import Counter
# str = 'I can because i think i can'
# counts = Counter(str.split())
# print(counts)